﻿using Mapster;
using RsCms.Application.Posts.Dto;
using RsCms.Core;
using RsCode;

namespace Plugin.Archives.Service
{
	
	public class ArchiveAppService
	{
		ArchiveDomainService archiveDomainService;
		IPostDomainService postDomainService;
		public ArchiveAppService(ArchiveDomainService archiveDomainService, IPostDomainService postDomainService)
		{
			this.archiveDomainService = archiveDomainService;
			this.postDomainService = postDomainService;
		}

		public async Task<PostsDto> SaveAsync(PostEditDto dto)
		{
			var posts = dto.Adapt<RsCms.Entities.Posts>();

			posts.Type = "archive";
			posts.Html = dto.Markdown;
			var info = await postDomainService.SaveAsync(posts);
			var target = info.Adapt<PostsDto>();
			return target;
		}

		public async Task<PostsDto> GetArchiveByTitleOrIdAsync(string idOrTitle)
		{
			var info = await archiveDomainService.GetArchiveAsync(idOrTitle);

			return info?.Adapt<PostsDto>();
		}

		public async Task<PageData<PostsDto>> PageArchiveAsync(int page, int pageSize, string date = null)
		{
			var dt = date != null ? date.Split(',') : null;
			var info = await archiveDomainService.PageArchiveAsync(page, pageSize, dt);
			return info.Adapt<PageData<PostsDto>>();
		}



		public async Task DeleteArchiveAsync(string postId)
		{
			await archiveDomainService.DeleteArchiveAsync(postId);
		}
	}
}
